package com.rtx.edi.platform.application.controller;

import java.util.List;
import java.util.Optional;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.application.dto.PlApplicationLinkDtlDto;
import com.rtx.edi.model.application.dto.PlApplicationLinkDto;
import com.rtx.edi.model.application.po.PlApplicationLink;
import com.rtx.edi.model.application.po.PlApplicationLinkDtl;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.application.service.PlApplicationLinkDtlService;
import com.rtx.edi.platform.application.service.PlApplicationLinkService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 应用关联表  前端控制器
 * </p>
 *
 * @author wq
 * @since 2023-03-14
 */
@Api(tags = {"应用关联管理"})
@RestController
@RequestMapping("/v1/plApplicationLink")
@CrossOrigin
public class PlApplicationLinkController {

    @Autowired
    private PlApplicationLinkService plApplicationLinkService;

    @Autowired
    private PlApplicationLinkDtlService plApplicationLinkDtlService;
    
    
    @ApiOperation(value = "应用关联_下拉框", notes = "应用关联_下拉框")
    @PostMapping("selectPlAppLinkSelector")
    public EdiResponse selectPlAppLinkSelector() {
    	return EdiResponse.success().data("item",plApplicationLinkService.selectPlAppLinkSelector());
    }
    
    @ApiOperation(value = "应用环境关联_下拉框", notes = "应用环境关联_下拉框")
    @PostMapping("selectPlAppLinkDtlSelector")
    public EdiResponse selectPlAppLinkDtlSelector(@RequestBody PlApplicationLinkDto dto) {
    	return EdiResponse.success().data("item",plApplicationLinkService.selectPlAppLinkDtlSelector(dto));
    }

    /**
     * 获取应用关联列表
     * @return
     */
    @ApiOperation(value = "应用关联列表", notes = "分页获取应用关联列表")
    @PostMapping("selectPlApplicationLinkList")
    public EdiResponse selectPlApplicationLinkList(@RequestBody PlApplicationLinkDto plApplicationLinkDto){
        PlApplicationLink plApplicationLink = new PlApplicationLink();
        BeanUtils.copyProperties(plApplicationLinkDto,plApplicationLink,"items");
        Page<PlApplicationLink> pageParam = new Page<>(plApplicationLinkDto.getPageNum(), plApplicationLinkDto.getPageSize());
        plApplicationLinkService.selectPlApplicationList(pageParam,plApplicationLink);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 据据id获取应用关联
     * @return
     */
    @ApiOperation(value = "获取应用关联", notes = "据据id获取应用关联")
    @PostMapping("selectPlApplicationLinkById")
    public EdiResponse selectPlApplicationLinkById(@RequestBody PlApplicationLinkDto plApplicationLinkDto){
        return EdiResponse.success().data("item",plApplicationLinkService.getById(plApplicationLinkDto.getId()));
    }

    /**
     * 根据应用关联id获取应用关联明细
     * @return
     */
    @ApiOperation(value = "获取应用关联明细", notes = "根据应用关联id获取应用关联明细")
    @PostMapping("selectPlApplicationLinkDtlByLinkId")
    public EdiResponse selectPlApplicationLinkDtlByLinkId(@RequestBody PlApplicationLinkDtlDto plApplicationLinkDtlDto){
        PlApplicationLinkDtl plApplicationLinkDtl = new PlApplicationLinkDtl();
        BeanUtils.copyProperties(plApplicationLinkDtlDto,plApplicationLinkDtl);
        Page<PlApplicationLinkDtl> pageParam = new Page<>(plApplicationLinkDtlDto.getPageNum(), plApplicationLinkDtlDto.getPageSize());
        plApplicationLinkDtlService.selectPlApplicationLinkDtlByLinkId(pageParam,plApplicationLinkDtl);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 新增应用关联
     * @param plApplicationLinkDto
     * @return
     */
    @ApiOperation(value = "新增应用关联", notes = "新增应用关联")
    @PostMapping("addPlApplicationLink")
    public EdiResponse addPlApplicationLink(@RequestBody PlApplicationLinkDto plApplicationLinkDto){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String companyid = (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
		plApplicationLinkDto.setCompanyId(Long.parseLong(companyid));
		
    	PlApplicationLink plApplicationLink = plApplicationLinkService.convertPlApplicationLink(plApplicationLinkDto);
        plApplicationLinkService.addPlApplicationLink(plApplicationLink);
        return EdiResponse.success().data("item",plApplicationLink);
    }

    /**
     * 新增应用关联明细
     * @param plApplicationLinkDtlDto
     * @return
     */
    @ApiOperation(value = "新增应用关联明细", notes = "新增应用关联明细")
    @PostMapping("addPlApplicationLinkDtl")
    public EdiResponse addPlApplicationLinkDtl(@RequestBody PlApplicationLinkDtlDto plApplicationLinkDtlDto){
        PlApplicationLinkDtl plApplicationLinkDtl = new PlApplicationLinkDtl();
        BeanUtils.copyProperties(plApplicationLinkDtlDto,plApplicationLinkDtl);
        plApplicationLinkService.addPlApplicationLinkDtl(plApplicationLinkDtl);
        return EdiResponse.success().data("item",plApplicationLinkDtl);
    }

    /**
     * 修改应用关联
     * @param plApplicationLinkDto
     * @return
     */
    @ApiOperation(value = "修改应用关联", notes = "根据ID修改应用关联")
    @PostMapping("editPlApplicationLink")
    public EdiResponse editPlApplicationLink(@RequestBody PlApplicationLinkDto plApplicationLinkDto){
        PlApplicationLink plApplicationLink = plApplicationLinkService.convertPlApplicationLink(plApplicationLinkDto);
        plApplicationLinkService.editPlApplicationLink(plApplicationLink);
        return EdiResponse.success();
    }

    /**
     * 修改应用关联明细
     * @param plApplicationLinkDtlDto
     * @return
     */
    @ApiOperation(value = "修改应用关联明细", notes = "根据ID修改应用关联明细")
    @PostMapping("editPlApplicationLinkDtl")
    public EdiResponse editPlApplicationLinkDtl(@RequestBody PlApplicationLinkDtlDto plApplicationLinkDtlDto){
        PlApplicationLinkDtl plApplicationLinkDtl = new PlApplicationLinkDtl();
        BeanUtils.copyProperties(plApplicationLinkDtlDto,plApplicationLinkDtl);
        plApplicationLinkService.editPlApplicationLinkDtl(plApplicationLinkDtl);
        return EdiResponse.success();
    }

    /**
     * 根据id删除应用关联
     * @param plApplicationLinkDto
     * @return
     */
    @ApiOperation(value = "删除应用关联", notes = "根据id删除应用关联", httpMethod = "POST")
    @PostMapping("deletePlApplicationLinkById")
    public EdiResponse deletePlApplicationLinkById(@RequestBody PlApplicationLinkDto plApplicationLinkDto) {
        plApplicationLinkService.deletePlApplicationLinkById(plApplicationLinkDto.getId());
        return EdiResponse.success();
    }

    /**
     * 根据id删除应用关联明细
     * @param plApplicationLinkDtlDto
     * @return
     */
    @ApiOperation(value = "删除应用关联明细", notes = "根据id删除应用关联明细", httpMethod = "POST")
    @PostMapping("deletePlApplicationLinkDtlById")
    public EdiResponse deletePlApplicationLinkDtlById(@RequestBody PlApplicationLinkDtlDto plApplicationLinkDtlDto) {
        PlApplicationLinkDtl plApplicationLinkDtl = new PlApplicationLinkDtl();
        BeanUtils.copyProperties(plApplicationLinkDtlDto,plApplicationLinkDtl);
        plApplicationLinkDtlService.deletePlApplicationLinkDtlById(plApplicationLinkDtl);
        return EdiResponse.success();
    }

    /**
     * 批量删除应用关联
     * @param list
     * @return
     */
    @ApiOperation(value = "批量删除应用关联", notes = "批量删除应用关联", httpMethod = "POST")
    @PostMapping("deletePlApplicationLinkByIds")
    public EdiResponse deletePlApplicationLinkByIds(@RequestBody List<PlApplicationLinkDtlDto> list){
        plApplicationLinkService.deletePlApplicationLinkByIds(list);
        return EdiResponse.success();
    }

}
